package com.gongj.resilience4j.service;

import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.retry.annotation.Retry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @program: cloudlx
 * @description:
 * @author: gongj
 * @Description: TODO
 * @create: 2020-06-19 23:59
 **/
@Service
@Retry(name = "retryA")
@CircuitBreaker(name = "cbA",fallbackMethod = "error")
public class resilience4jServcie {

    @Autowired
    RestTemplate restTemplate;

    public String getUser(){
        for (int i = 0; i < 5; i++) {
            this.restTemplate.getForObject("http://appUser/api/user",String.class);
        }
         return "success";
       // return restTemplate.getForObject("http://localhost:2020/api/user",
                //String.class);
    }

    public String error(Throwable t){
        System.out.println(t.getMessage() + "======");
        System.out.println(t + "======");
        return "error";
    }
}
